<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis哈希槽机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 700;
        }
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            width: 48px;
            height: 48px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            margin-bottom: 16px;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .info-badge {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 14px;
            font-weight: 500;
            display: inline-block;
            margin-right: 8px;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">Redis哈希槽机制</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解分布式缓存的核心设计</p>
            <div class="flex justify-center space-x-4">
                <span class="info-badge"><i class="fas fa-server mr-2"></i>16384个哈希槽</span>
                <span class="info-badge"><i class="fas fa-network-wired mr-2"></i>高可用集群</span>
                <span class="info-badge"><i class="fas fa-tachometer-alt mr-2"></i>高性能分布式</span>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">R</span>edis集群使用哈希槽（Hash Slot）来管理数据分布，整个集群被划分为固定的<span class="highlight-box">16384个哈希槽</span>。当我们在集群中存储一个键时，Redis会先对键进行哈希运算，得到一个哈希值。然后，Redis将该哈希值对16384取模，以确定该键应该放在哪个哈希槽中。这样，每个键都有唯一的槽位标识，确保了数据的均匀分布。
            </p>
        </div>

        <!-- Hash Slot Visualization -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 gradient-text">哈希槽分布可视化</h2>
            <div class="mermaid">
                graph TB
                    subgraph "Redis集群"
                        A[客户端请求] --> B{哈希计算}
                        B --> C[槽位0-5460<br/>节点1]
                        B --> D[槽位5461-10922<br/>节点2]
                        B --> E[槽位10923-16383<br/>节点3]
                        
                        style A fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
                        style B fill:#667eea,stroke:#764ba2,stroke-width:2px,color:#fff
                        style C fill:#4facfe,stroke:#00f2fe,stroke-width:2px,color:#fff
                        style D fill:#43e97b,stroke:#38f9d7,stroke-width:2px,color:#fff
                        style E fill:#fa709a,stroke:#fee140,stroke-width:2px,color:#fff
                    end
            </div>
        </div>

        <!-- Core Mechanism -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <div class="content-card rounded-2xl shadow-xl p-8">
                <div class="feature-icon">
                    <i class="fas fa-cogs"></i>
                </div>
                <h3 class="text-2xl font-bold mb-4">哈希槽机制</h3>
                <p class="text-gray-700 leading-relaxed">
                    在Redis集群中，总共设定了<strong>16384个固定的哈希槽</strong>（Hash Slot），每个槽用于存放数据的一部分。Redis集群并没有像一致性哈希那样直接将数据分布到节点上，而是先将数据映射到固定数量的槽中，再将这些槽分配给不同的节点。
                </p>
            </div>
            
            <div class="content-card rounded-2xl shadow-xl p-8">
                <div class="feature-icon">
                    <i class="fas fa-question-circle"></i>
                </div>
                <h3 class="text-2xl font-bold mb-4">为什么选择16384？</h3>
                <p class="text-gray-700 leading-relaxed">
                    16384这个数量既保证了足够的分布精度，又不会引入过多的管理开销。槽的数量固定，可以避免动态增加槽位所带来的复杂性。基于大多数机器的运算能力，这个数能够平衡操作成本和系统性能。
                </p>
            </div>
        </div>

        <!-- Working Flow -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 gradient-text">哈希槽的工作流程</h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="text-4xl font-bold gradient-text mb-2">01</div>
                    <h4 class="text-xl font-semibold mb-2">均匀分布</h4>
                    <p class="text-gray-700">16384个槽位确保了数据在不同节点间可以更均匀地分配，避免数据和负载集中在某些节点。</p>
                </div>
                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 p-6 rounded-xl">
                    <div class="text-4xl font-bold gradient-text mb-2">02</div>
                    <h4 class="text-xl font-semibold mb-2">可伸缩性</h4>
                    <p class="text-gray-700">当有新的节点加入或退出集群时，Redis只需重新分配一部分槽位，大幅降低了数据迁移的成本。</p>
                </div>
                <div class="bg-gradient-to-br from-green-50 to-teal-50 p-6 rounded-xl">
                    <div class="text-4xl font-bold gradient-text mb-2">03</div>
                    <h4 class="text-xl font-semibold mb-2">高效路由</h4>
                    <p class="text-gray-700">客户端可以在本地预先缓存槽和节点的映射关系，从而实现请求的快速路由，减少网络延迟。</p>
                </div>
            </div>
        </div>

        <!-- Data Migration Process -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 gradient-text">数据迁移过程</h2>
            <div class="mermaid">
                graph LR
                    A[选择迁移槽] --> B[数据转移]
                    B --> C[数据校验]
                    C --> D[更新映射表]
                    D --> E[完成迁移]
                    
                    style A fill:#667eea,stroke:#764ba2,stroke-width:2px,color:#fff
                    style B fill:#4facfe,stroke:#00f2fe,stroke-width:2px,color:#fff
                    style C fill:#43e97b,stroke:#38f9d7,stroke-width:2px,color:#fff
                    style D fill:#fa709a,stroke:#fee140,stroke-width:2px,color:#fff
                    style E fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
            </div>
            <div class="mt-6 space-y-4">
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                    <p class="text-gray-700"><strong>迁移的原子性与一致性保障：</strong>数据迁移的每个操作是基于单槽的原子性，避免数据同步冲突。</p>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                    <p class="text-gray-700"><strong>渐进迁移策略：</strong>将迁移过程分批执行，避免大批量数据一次性迁移带来的性能开销。</p>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                    <p class="text-gray-700"><strong>容错机制：</strong>若迁移期间出现故障，Redis会自动重试并在必要时执行回滚操作。</p>
                </div>
            </div>
        </div>

        <!-- Comparison with Consistent Hashing -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 gradient-text">与一致性哈希的比较</h2>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="border-b-2 border-purple-200">
                            <th class="text-left py-3 px-4">特性</th>
                            <th class="text-left py-3 px-4">Redis哈希槽</th>
                            <th class="text-left py-3 px-4">一致性哈希</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="border-b border-gray-200 hover:bg-purple-50 transition-colors">
                            <td class="py-3 px-4 font-semibold">设计理念</td>
                            <td class="py-3 px-4">静态分配16384个槽位</td>
                            <td class="py-3 px-4">动态环形空间映射</td>
                        </tr>
                        <tr class="border-b border-gray-200 hover:bg-purple-50 transition-colors">
                            <td class="py-3 px-4 font-semibold">数据分布</td>
                            <td class="py-3 px-4">槽位粒度迁移</td>
                            <td class="py-3 px-4">局部数据迁移</td>
                        </tr>
                        <tr class="border-b border-